package com.cjhhv.visual.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cjhhv.base.model.ResponseDataModel;
import com.cjhhv.base.util.ResponseDataUtil;
import com.cjhhv.visual.model.DashboardCategoryModel;
import com.cjhhv.visual.service.DashboardCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dashboardCategory")
public class DashboardCategoryController {

    private DashboardCategoryService dashboardCategoryService;

    @PostMapping("/queryDashboardCategoryList")
    @ResponseBody
    public ResponseDataModel queryDashboardCategoryList(@RequestBody DashboardCategoryModel parameter){
        List<DashboardCategoryModel> list = this.dashboardCategoryService.queryDashboardCategoryList(parameter);
        return ResponseDataUtil.createResponseData(true,"查询成功!",list);
    }

    @GetMapping("/queryDashboardCategoryById")
    @ResponseBody
    public ResponseDataModel queryDashboardCategoryById(@RequestParam("id") String id ){
        DashboardCategoryModel dashboardCategoryModel = this.dashboardCategoryService.queryDashboardCategoryById(id);
        if(ObjectUtil.isNotEmpty(dashboardCategoryModel)){
            return ResponseDataUtil.createResponseData(true,"查询成功!",dashboardCategoryModel);
        }else{
            return ResponseDataUtil.createResponseData(false,"查询失败!",null);
        }
    }

    @PostMapping("/saveDashboardCategory")
    @ResponseBody
    public ResponseDataModel saveDashboardCategory(@RequestBody DashboardCategoryModel parameter){
        int count = this.dashboardCategoryService.queryDashboardCategoryIsExist(parameter);
        if(count>0){
            return ResponseDataUtil.createResponseData(false,parameter.getDashboardCategoryLabel()+"已存在！",null);
        }
        DashboardCategoryModel dashboardCategoryModel = this.dashboardCategoryService.saveDashboardCategory(parameter);
        if(ObjectUtil.isEmpty(dashboardCategoryModel)){
            return ResponseDataUtil.createResponseData(false,"保存失败!",null);
        }
        return ResponseDataUtil.createResponseData(true,"保存成功!",dashboardCategoryModel);
    }

    @PutMapping("/updateDashboardCategoryById")
    @ResponseBody
    public ResponseDataModel updateDashboardCategoryById(@RequestBody DashboardCategoryModel parameter){
        int count = this.dashboardCategoryService.queryDashboardCategoryIsExist(parameter);
        if(count>0){
            return ResponseDataUtil.createResponseData(false,parameter.getDashboardCategoryLabel()+"已存在！",null);
        }
        DashboardCategoryModel dashboardCategoryModel = this.dashboardCategoryService.updateDashboardCategoryById(parameter);
        if(ObjectUtil.isEmpty(dashboardCategoryModel)){
            return ResponseDataUtil.createResponseData(false,"保存失败!",null);
        }
        return ResponseDataUtil.createResponseData(true,"保存成功!",dashboardCategoryModel);
    }

    @PostMapping("/deleteDashboardCategoryByIds")
    @ResponseBody
    public ResponseDataModel deleteDashboardCategoryByIds(@RequestBody DashboardCategoryModel parameter) {
        int count =  this.dashboardCategoryService.deleteDashboardCategoryByIds(parameter);
        if(count>0){
            return ResponseDataUtil.createResponseData(true,"删除成功!",null);
        }else {
            return ResponseDataUtil.createResponseData(false,"删除失败!",null);
        }
    }

    public DashboardCategoryService getDashboardCategoryService() {
        return dashboardCategoryService;
    }

    @Autowired
    public void setDashboardCategoryService(DashboardCategoryService dashboardCategoryService) {
        this.dashboardCategoryService = dashboardCategoryService;
    }
}
